package com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Deptinfos;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.DoctorInfo;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.entity.IconInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.entity.QualityAssuranceDoctorInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.entity.QualityAssuranceInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.entity.SearchQualityAssuranceInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.service.IDentalImageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 质量管理控制器
 *
 * @author fengshuonan
 * @Date 2019-09-01 15:34:05
 */
@Controller
@RequestMapping("/qualityAssurances")
public class DentalImageController extends BaseController {
    @Autowired
    IDentalImageService service;

    @Autowired
    private SqlMapper sqlMapper;


    private String PREFIX = "/qualityAssurance/dentalImage/";

    private String PREFIS = "/qualityAssurance/";

    /**
     * 跳转到智能打分首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIX + "qualityAssurances.html";
    }

    /**
     * 跳转到正畸影像
     */
    @RequestMapping(value = "/qualityAssurances1")
    public String qualityAssurances1(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        if (ShiroKit.getUser().getDoctorInfoList() == null) {
            String doctorListSql = " select a.id as id,a.account as doctor from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on find_in_set(c.id,a.roleid) > 0 " +
                    " where b.abbreviation='" + ShiroKit.getUser().getAbbreviation() + "' and (c.name='医生' or c.name='洁牙师') and a.status='1';";
            List<DoctorInfo> doctorInfoList = sqlMapper.selectList(doctorListSql, DoctorInfo.class);
            ShiroKit.getUser().setDoctorInfoList(doctorInfoList);
        }
        model.addAttribute("doctorList", ShiroKit.getUser().getDoctorInfoList());
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIS + "qualityAssurances1.html";
    }

    /**
     * 跳转到根管影像
     */
    @RequestMapping(value = "/qualityAssurances2")
    public String qualityAssurances2(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIS + "qualityAssurances2.html";
    }

    /**
     * 跳转到洁牙影像
     */
    @RequestMapping(value = "/qualityAssurances3")
    public String qualityAssurances3(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIS + "qualityAssurances3.html";
    }

    /**
     * 跳转到牙周影像
     */
    @RequestMapping(value = "/qualityAssurances4")
    public String qualityAssurances4(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIS + "qualityAssurances4.html";
    }

    /**
     * 跳转到种植影像
     */
    @RequestMapping(value = "/qualityAssurances5")
    public String qualityAssurances5(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIS + "qualityAssurances5.html";
    }

    /**
     * 跳转到修复影像
     */
    @RequestMapping(value = "/qualityAssurances6")
    public String qualityAssurances6(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIS + "qualityAssurances6.html";
    }

    /**
     * 跳转到缓存影像
     */
    @RequestMapping(value = "/qualityAssurances9")
    public String qualityAssurances9(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getArealist() == null) {
            String sql1 = "select area_abbreviation as areaAbbreviation,area as area from dept a where length(a.pids)>4 " +
                    " and version='" + ShiroKit.getUser().getCompanyVersion() + "' group by a.area order by a.num desc";
            List<Deptinfos> arealist = sqlMapper.selectList(sql1, Deptinfos.class);
            ShiroKit.getUser().setArealist(arealist);
        }
        model.addAttribute("arealist", ShiroKit.getUser().getArealist());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("areaAbbreviation", ShiroKit.getUser().getAreaAbbreviation());
        model.addAttribute("userid", ShiroKit.getUser().getId());
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("abbreviation", ShiroKit.getUser().getAbbreviation());
        return PREFIS + "qualityAssurances9.html";
    }

    /**
     * 获取质量管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(SearchQualityAssuranceInfo info, HttpServletRequest request) {
        Page<QualityAssuranceInfo> page = new PageFactory<QualityAssuranceInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<QualityAssuranceInfo> list = service.selectPageListByQuality(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.selectPageListByQuality(page, info);
            }
            page.setTotal(service.getQueryPageCount(page, info));
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 获取质量管理列表
     */
    @RequestMapping(value = "/getList/{reservationsId}")
    @ResponseBody
    public List<QualityAssuranceInfo> getList(@PathVariable String reservationsId, Model model) throws Exception {
        if (ToolUtil.isEmpty(reservationsId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "select t1.id as id,t1.outpatient_name as outpatientName,t1.add_name as addName ,t1.add_time as addTime,t1.status as status, " +
                "        t1.customer_name as customerName,t1.customer_phone as customerPhone,t1.num as num,t1.reservations_id as reservationsId, " +
                "        t1.tooth_position as toothPosition,t1.score as score,t1.image_url as imageUrl,t1.doctor as doctor,t3.create_time as createTime, " +
                "        t3.A1 as A1,t3.A2 as A2,t3.A3 as A3,t3.A4 as A4,t3.A5 as A5,t3.A6 as A6,t3.A7 as A7," +
                "        t3.B1 as B1,t3.B2 as B2,t3.B3 as B3,t3.B4 as B4,t3.B5 as B5,t3.B6 as B6,t3.B7 as B7,t1.icon_name as iconName, " +
                "        (SELECT score from inco_info_standard limit 1 ) as standardScore,t3.photo_type as photoType, " +
                "        (select sum( " +
                "        g.A1 / 3 + g.A2 / 3 + g.A3 / 3 + " +
                "        IF (g.photo_type IN(1, 3), 0, g.A4) + " +
                "        IF (g.photo_type IN(1, 2), 0, g.A5) + " +
                "        IF (g.photo_type IN(1, 2), 0, g.A6) + " +
                "        IF (g.photo_type IN(1, 2), 0, g.A7) " +
                "        ) from (SELECT " +
                "        d.photo_type as photo_type,d.A1,d.A2,d.A3,d.A4,d.A5,d.A6,d.A7,e.num " +
                "        FROM icon_infos e LEFT JOIN intelligent_score_details_info2 d ON e.id = d.id2 GROUP BY e.id ) g WHERE  g.num = t1.num) AS scoreTOTAL " +
                "        from icon_infos t1 left JOIN intelligent_score_details_info2 t3 on t1.id= t3.id2 " +
                "        where 1=1 and t1.reservations_id='" + reservationsId + "'" +
                "        group by t1.id " +
                "        order by t1.add_time desc,t1.icon_name='术后',t1.icon_name='术中',t1.icon_name='术前'";
        List<QualityAssuranceInfo> qualityAssuranceInfoList = sqlMapper.selectList(sql, QualityAssuranceInfo.class);
        return qualityAssuranceInfoList;
    }

    /**
     * 统计查询
     */
    @RequestMapping(value = "/total", method = RequestMethod.GET)
    @ResponseBody
    public List<QualityAssuranceInfo> change(String status, String startUploadTime, String endUploadTime, String addName, String arrivalStore) throws Exception {
        String contitionInfo = "";
        if (StringUtils.isNotBlank(startUploadTime)) {
            contitionInfo += " and t.add_time >='" + startUploadTime + "' ";
        }
        if (StringUtils.isNotBlank(endUploadTime)) {
            contitionInfo += " and t.add_time <='" + endUploadTime + "' ";
        }
        if (StringUtils.isNotBlank(addName)) {
            contitionInfo += " and t.add_name='" + addName + "' ";
        }
        if (StringUtils.isNotBlank(arrivalStore)) {
            contitionInfo += " and t.outpatient_name='" + arrivalStore + "' ";
        }
        String sql = "select count(*) as countNum from (select t.id from icon_infos t where 1=1 " + contitionInfo + ") t1 " +
                " union all select count(*) as countNum from (select t.id from  icon_infos t where t.status=1   " + contitionInfo + ") t1" +

                " union all  select count(*) as countNum from (select t.id from  icon_infos t  where t.status =2   " + contitionInfo + ") t1";

        List<QualityAssuranceInfo> tongJiInfoList1 = sqlMapper.selectList(sql, QualityAssuranceInfo.class);
        return tongJiInfoList1;
    }

    /**
     * 打分详情
     */
    @RequestMapping(value = "/score_detailsed/{num}/{scoringStatus}")
    public String score_detailsed(@PathVariable String num,@PathVariable String scoringStatus, Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ToolUtil.isEmpty(num)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "select t1.id as id,t3.outpatient_name as outpatientName,t3.add_name as addName ,t3.add_time as addTime,t3.status as status, " +
                " t3.customer_name as customerName,t3.customer_phone as customerPhone,t3.num as num,t3.reservations_id as reservationsId, " +
                " t3.tooth_position as toothPosition,t1.score as score,t3.image_url as imageUrl,t3.doctor as doctor,t1.create_time as createTime, " +
                " t1.A1 as A1,t1.A2 as A2,t1.A3 as A3,t1.A4 as A4,t1.A5 as A5,t1.A6 as A6,t1.A7 as A7,t1.scoring_status as scoringStatus,t3.icon_name as iconName ," +
                "FORMAT((SELECT sum(t4.A1)/3 from intelligent_score_details_info2 t4 left JOIN icon_infos t5 on t4.id2= t5.id where  t5.num=t3.num),2) as A8," +
                "FORMAT((SELECT sum(t4.A2)/3 from intelligent_score_details_info2 t4 left JOIN icon_infos t5 on t4.id2= t5.id where  t5.num=t3.num),2) as A9," +
                "FORMAT((SELECT sum(t4.A3)/3 from intelligent_score_details_info2 t4 left JOIN icon_infos t5 on t4.id2= t5.id where  t5.num=t3.num),2) as A10," +
                "(SELECT t4.A4 from intelligent_score_details_info2 t4 left JOIN icon_infos t5 on t4.id2= t5.id where t5.num=t3.num and t5.icon_name1=2) as A11," +
                "(SELECT t4.A5 from intelligent_score_details_info2 t4 left JOIN icon_infos t5 on t4.id2= t5.id where t5.num=t3.num and t5.icon_name1=1) as A12," +
                "(SELECT t4.A6 from intelligent_score_details_info2 t4 left JOIN icon_infos t5 on t4.id2= t5.id where t5.num=t3.num and t5.icon_name1=1) as A13," +
                "(SELECT t4.A7 from intelligent_score_details_info2 t4 left JOIN icon_infos t5 on t4.id2= t5.id where t5.num=t3.num and t5.icon_name1=1) as A14 " +
                "from intelligent_score_details_info2 t1 left JOIN icon_infos t3 on t1.id2= t3.id " +
                " where  t3.num='" + num + "' order by t1.photo_type ASC";
        List<QualityAssuranceInfo> qualityAssuranceInfoList = sqlMapper.selectList(sql, QualityAssuranceInfo.class);
        //查询患者有几个医生评分
        String sql2= "SELECT tt.id AS id,tt.num AS num, tt.create_time as createTime, tt.doctor as doctor FROM (SELECT * from intelligent_score_doctor_info t ORDER BY t.create_time DESC LIMIT 1000 ) tt  WHERE tt.num = '"+num+"' group by tt.doctor ";
        List<QualityAssuranceDoctorInfo> qualityDoctorInfoList = sqlMapper.selectList(sql2, QualityAssuranceDoctorInfo.class);
        for(int i = 0; i < qualityDoctorInfoList.size(); i++){
            try {
                List<QualityAssuranceDoctorInfo> currentList = getQualityDoctorInfoList(qualityDoctorInfoList.get(i).getDoctor(),qualityDoctorInfoList.get(i).getCreateTime());
                qualityDoctorInfoList.get(i).setCurrentList(currentList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        model.addAttribute("qualityAssuranceInfoList", qualityAssuranceInfoList);
        model.addAttribute("qualityDoctorInfoList", qualityDoctorInfoList);
        model.addAttribute("num", num);
        model.addAttribute("scoringStatus", scoringStatus);
        return PREFIX + "qualitiyDetailsed.html";
    }

    public List<QualityAssuranceDoctorInfo> getQualityDoctorInfoList(String doctor,String createTime) throws Exception {
        if (ToolUtil.isEmpty(doctor)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "SELECT t1.id AS id,t1.num AS num,t1.create_time as createTime,t1.id2 as id2,t1.photo_type as photoType,t1.A1 AS A1,t1.A2 AS A2,t1.A3 AS A3,t1.A4 AS A4,t1.A5 AS A5,t1.A6 AS A6,t1.A7 AS A7," +
                "FORMAT((SELECT sum(t4.A1) / 3 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"'),2) AS A8," +
                "FORMAT((SELECT sum(t4.A2) / 3 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"'),2) AS A9," +
                "FORMAT((SELECT sum(t4.A3) / 3 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"'),2) AS A10," +
                "(SELECT t4.A4 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术中') AS A11," +
                "(SELECT t4.A5 FROM intelligent_score_doctor_info t4 WHERE t4.doctor =t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前') AS A12," +
                "(SELECT t4.A6 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前') AS A13," +
                "(SELECT t4.A7 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前') AS A14," +
                "((SELECT t4.A4 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术中')-(SELECT tT.A4 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=2)) AS A15," +
                "((SELECT t4.A5 FROM intelligent_score_doctor_info t4 WHERE t4.doctor =t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前')-(SELECT tT.A5 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=1)) AS A16," +
                "((SELECT t4.A6 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前')-(SELECT tT.A6 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=1)) AS A17," +
                "((SELECT t4.A7 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前')-(SELECT tT.A7 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=1)) AS A18," +
                "(((SELECT t4.A4 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术中')+(SELECT t4.A5 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前')+" +
                "(SELECT t4.A6 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前')+(SELECT t4.A7 FROM intelligent_score_doctor_info t4 WHERE t4.doctor = t1.doctor and t4.create_time='"+createTime+"' AND t4.photo_type = '术前'))-" +
                "((SELECT tT.A4 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=2)+(SELECT tT.A5 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=1)+" +
                "(SELECT tT.A6 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=1)+(SELECT tT.A7 from intelligent_score_details_info2 tT left JOIN icon_infos t5 on tT.id2= t5.id where t5.num=t1.num and t5.icon_name1=1))) as A19  " +
                "FROM intelligent_score_doctor_info t1 WHERE t1.doctor = '"+doctor+"' and t1.create_time='"+createTime+"' " +
                "ORDER BY case  " +
                "when photo_type = '术前' then 1 " +
                "when photo_type = '术中' then 2 " +
                "when photo_type = '术后' then 3 " +
                "end";
        List<QualityAssuranceDoctorInfo> assuranceDoctorInfoList = sqlMapper.selectList(sql, QualityAssuranceDoctorInfo.class);
        return assuranceDoctorInfoList;
    }
    /**
     * 人工打分
     */
    @RequestMapping(value = "/artificial_scoring/{num}")
    public String artificial_scoring(@PathVariable String num, Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ToolUtil.isEmpty(num)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Date date = new Date();
        long number = date.getTime();
        String sql1 = "select t.icon_name as iconName,t.image_url as imageUrl,t.pay_service_id as payServiceId,t.score as score," +
                "t.cause as cause,t.status as status from icon_infos t  where t.num='" + num + "';";
        List<QualityAssuranceInfo> qualityArtificialScoringInfoList = sqlMapper.selectList(sql1, QualityAssuranceInfo.class);
        model.addAttribute("qualityArtificialScoringInfoList", qualityArtificialScoringInfoList);
        model.addAttribute("num", num);
        model.addAttribute("number", number);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "qualitiyArtificialScoring.html";
    }

    /**
     * 人工打分提交
     */
    @RequestMapping(value = "/scoringSure")
    @ResponseBody
    public int ensure(@RequestBody List<QualityAssuranceInfo> operativeList, HttpServletRequest request) {
        String scoreType="";
        for (int i = 0; i < operativeList.size(); i++) {
            QualityAssuranceInfo info = operativeList.get(i);
            String curId ="select id,num from icon_infos where num='"+info.getNum()+"' and icon_name='"+info.getType()+"'";
            IconInfo iconInfo = sqlMapper.selectOne(curId,IconInfo.class);
//            String sql = " update intelligent_score_details_info2 a set a.B1='"+info.getList().get(0)+"',a.B2='"+info.getList().get(1)+"', " +
//                    " a.B3='"+info.getList().get(2)+"',a.B4='"+info.getList().get(3)+"', a.B5='"+info.getList().get(4)+"',a.B6='"+info.getList().get(5)+"'," +
//                    "a.B7='"+info.getList().get(6)+"' ,a.update_time='"+DateTimeKit.now()+"' where id2='"+iconInfo.getId()+"'";
//            sqlMapper.update(sql);
            String insertSql ="insert into intelligent_score_doctor_info (number,num,doctor,photo_type,id2,A1,A2,A3,A4,A5,A6,A7,create_time) values('"+info.getNumber()+"','"+info.getNum()+"','"+ShiroKit.getUser().getAccount()+"','"+info.getType()+"'," +
                    "'"+iconInfo.getId()+"','"+info.getList().get(0)+"','"+info.getList().get(1)+"','"+info.getList().get(2)+"','"+info.getList().get(3)+"','"+info.getList().get(4)+"','"+info.getList().get(5)+"','"+info.getList().get(6)+"','"+DateTimeKit.now()+"')";
            sqlMapper.insert(insertSql);
        }
        return 1;
    }

    /**
     * 打分详情
     */
    @RequestMapping(value = "/score_details/{num}")
    public String score_details(@PathVariable String num, Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ToolUtil.isEmpty(num)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql1 = "select t.icon_name as iconName,t.image_url as imageUrl,t.pay_service_id as payServiceId,t.score as score," +
                "t.cause as cause,t.status as status from icon_infos t  where t.num='" + num + "';";
        List<QualityAssuranceInfo> qualityAssuranceInfoList = sqlMapper.selectList(sql1, QualityAssuranceInfo.class);
        model.addAttribute("qualityAssuranceInfoList", qualityAssuranceInfoList);
        model.addAttribute("num", num);
        return PREFIX + "qualitiyDetails.html";
    }

    @RequestMapping(value = "/getStatus", method = RequestMethod.POST)
    @ResponseBody
    public int getStatus(String num) {
        String sql1 = "select t.id,t1.update_time as updateTime from icon_infos t left join intelligent_score_details_info2 t1 on t.id = t1.id2 where t.num='" + num + "' order by icon_name1 desc;";
        List<QualityAssuranceInfo> infoList = sqlMapper.selectList(sql1, QualityAssuranceInfo.class);
        if(infoList.get(0).getUpdateTime()==null || infoList.get(0).getUpdateTime()==""||infoList.get(0).getUpdateTime()=="undefined"){
            return 1;
        }else{
            return 0;
        }
    }

    @RequestMapping(value = "/getScoreStatus", method = RequestMethod.POST)
    @ResponseBody
    public int getScoreStatus(String scoreStatus, String num,String id) {
        if("0".equals(scoreStatus)){
            String sql1 = "select id from icon_infos t  where t.num='" + num + "' order by icon_name1 desc;";
            List<QualityAssuranceInfo> infoList = sqlMapper.selectList(sql1, QualityAssuranceInfo.class);
            for(QualityAssuranceInfo info:infoList){
                String updateSql="update intelligent_score_details_info2 a set a.scoring_status='"+scoreStatus+"' where id2='"+info.getId()+"'";
                sqlMapper.update(updateSql);
            }
            return 1;
        }
        else{
            String sql = "select id,number from intelligent_score_doctor_info where id ='"+id+"'";
            QualityAssuranceDoctorInfo info = sqlMapper.selectOne(sql,QualityAssuranceDoctorInfo.class);
            String sqlList ="select * from intelligent_score_doctor_info where number='"+info.getNumber()+"' ORDER BY case " +
                    "when photo_type = '术前' then 1  " +
                    "when photo_type = '术中' then 2  " +
                    "when photo_type = '术后' then 3 " +
                    "end ";
            List<QualityAssuranceDoctorInfo> infoList = sqlMapper.selectList(sqlList,QualityAssuranceDoctorInfo.class);
            for(QualityAssuranceDoctorInfo infos:infoList){
                String updateSql="update intelligent_score_details_info2 a set a.scoring_status='"+scoreStatus+"',a.B1='"+infos.getA1()+"'," +
                        "a.B2='"+infos.getA2()+"',a.B3='"+infos.getA3()+"',a.B4='"+infos.getA4()+"',a.B5='"+infos.getA5()+"',a.B6='"+infos.getA6()+"',a.B7='"+infos.getA7()+"' where id2='"+infos.getId2()+"'";
                sqlMapper.update(updateSql);
            }
            return 1;
        }
    }
}
